#include <stddef.h> /* NULL */
#include <stdio.h>
#include <assert.h> /* assert() */
#include <string.h> /* strlen() */
#include <ctype.h>  /* tolower() */
#include "header/errors.h"
#include "header/headers.h"

header* loadHeader(char *headerName, unsigned int headerType) {
    assert(headerName != NULL);

    int nameMemorySize;
    char *tmp;
    char *headerContent[2];
    FILE *headerFile;
    char fileName[50] = "includes/";
    fileName[10] = '\0';

    if(headerType == 0 || headerType == 1) {
        header *loadedHead = (header*)mallocError(malloc(sizeof(header)));
        loadedHead->type = headerType;

        nameMemorySize = ((strlen(headerName) + 1) * sizeof(char));
        loadedHead->headerName = (char*)mallocError(malloc(nameMemorySize));

        for(tmp = headerName; *tmp; tmp++) {
            *tmp = tolower(*tmp);
        }

        /*strcat(fileName, headerName);*/
        strcat(fileName, "test");
        strcat(fileName, ".h");

        strcpy(loadedHead->headerName, headerName);

        headerFile = fopen(fileName, "r");
        if(headerFile == NULL) { printFileError(1, fileName); }

        loadToMem(fileName, &headerContent);
        loadedHead->keywords = *headerContent;

        return loadedHead;
    }

    printError(7); /* Unknown header type */
    return NULL;
}
